package com.fancy.learncenter.ui.activity;

import android.os.Bundle;
import android.support.v4.view.ViewPager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.fancy.learncenter.R;
import com.fancy.learncenter.bean.BaseBean;
import com.fancy.learncenter.bean.CartoonStudentSubjectiveDataBean;
import com.fancy.learncenter.bean.ListenSpeakDetailDataBean;
import com.fancy.learncenter.net.CustomNetCallBack;
import com.fancy.learncenter.net.CustomNetSubscriber;
import com.fancy.learncenter.net.HttpMehtod;
import com.fancy.learncenter.ui.activity.base.BaseActivity;
import com.fancy.learncenter.ui.adapter.FragPagerAdapter;
import com.fancy.learncenter.ui.fragment.BaseFragment;
import com.fancy.learncenter.ui.fragment.CartoonExpandVideoFragment;
import com.fancy.learncenter.ui.fragment.CartoonPicSpeakFragment;
import com.fancy.learncenter.ui.fragment.CartoonSentenceSpeakFragment;
import com.fancy.learncenter.ui.fragment.CartoonSongFragment;
import com.fancy.learncenter.ui.fragment.CartoonWordSpeakFragment;
import com.fancy.learncenter.ui.view.CartoonCompletePopupWindow;
import com.fancy.learncenter.utils.LogUtil;
import com.fancy.learncenter.utils.NetUtil;
import com.fancy.learncenter.utils.Utils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;
import okhttp3.ResponseBody;
import retrofit2.Response;

public class ExpandWorkActivity extends BaseActivity implements CartoonWordSpeakFragment.OnWorkSpeakListener {
    public static String PACKAGE_ID = "PACKAGE_ID";
    public static String PACKAGE_RESOURCEPATH = "PACKAGE_RESOURCEPATH";
    @Bind(R.id.recyclerView)
    RecyclerView recyclerView;
    @Bind(R.id.commit_image)
    ImageView commitImage;
    @Bind(R.id.rootView)
    LinearLayout rootView;
    @Bind(R.id.finish_work_num)
    TextView finishWorkNum;
    @Bind(R.id.previous_question)
    ImageView previousQuestion;
    @Bind(R.id.next_question)
    ImageView nextQuestion;

    private String speakPackageId = "";
    private String packagePath = "";

    List<BaseFragment> fragments;
    FragPagerAdapter mFragmentAdapteradapter;

    public int completeNum = 0;
    @Bind(R.id.work_title)
    TextView workTitle;


    public int totalNum = 4;
    @Bind(R.id.work_current_num)
    TextView workCurrentNum;
    @Bind(R.id.work_total_num)
    TextView workTotalNum;
    @Bind(R.id.view_pager)
    ViewPager viewPager;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setCartoonContentView(R.layout.activity_every_day_work, Status.STATUS);
        ButterKnife.bind(this);

        speakPackageId = getIntent().getStringExtra(PACKAGE_ID);
        packagePath = getIntent().getStringExtra(PACKAGE_RESOURCEPATH);

        fragments = new ArrayList<>();

        previousQuestion.setVisibility(View.GONE);

        viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {

                if (position == 0) {
                    previousQuestion.setVisibility(View.GONE);
//                    previous_question
                } else {
                    previousQuestion.setVisibility(View.VISIBLE);
                }

                if (position == fragments.size() - 1) {
                    nextQuestion.setVisibility(View.GONE);
                } else {
                    nextQuestion.setVisibility(View.VISIBLE);
                }


                changeUi(false);
                workCurrentNum.setText("" + (position + 1));
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });

        commitImage.setVisibility(View.GONE);
        getDatas();

    }


    public void changeUi(boolean isRecord) {
        LogUtil.MyLog("changeUi", "======changeUi==" + isRecord);
        if (isRecord) {
            commitImage.setImageResource(R.mipmap.commit_light);
            commitImage.setClickable(true);
        } else {
            commitImage.setImageResource(R.mipmap.commit_gray);
            commitImage.setClickable(false);
        }
    }


    @OnClick({R.id.previous_question, R.id.commit_image, R.id.next_question})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.previous_question:
                viewPager.setCurrentItem(viewPager.getCurrentItem() - 1, true);
                break;
            case R.id.commit_image:

                BaseFragment baseFragment = (BaseFragment) mFragmentAdapteradapter.currentFragment;
                baseFragment.fmCallBack();
                break;
            case R.id.next_question:
                viewPager.setCurrentItem(viewPager.getCurrentItem() + 1, true);
                break;
        }
    }


    public void leftQues() {
        viewPager.setCurrentItem(viewPager.getCurrentItem() + 1, true);
    }

    public void rightQues() {
        viewPager.setCurrentItem(viewPager.getCurrentItem() + 1, true);
    }

    private void getDatas() {
        Map map = new HashMap();
        map.put("speakPackageId", speakPackageId);
        HttpMehtod.getInstance().speakPackageDetail(map, new CustomNetSubscriber(this) {
            @Override
            public void onNext(Response<ResponseBody> responseBodyResponse) {

                BaseBean<ListenSpeakDetailDataBean> baseBean = Utils.fromJsonObject(NetUtil.getResResult(responseBodyResponse), ListenSpeakDetailDataBean.class);
                //数据业务逻辑处理
                CustomNetCallBack callBack = new CustomNetCallBack(getContext(), baseBean);
                callBack.setCallBack(new CustomNetCallBack.NetCallBack<ListenSpeakDetailDataBean>() {
                    @Override
                    public void success(ListenSpeakDetailDataBean dataBean) {
                        Utils.saveSudentKeyValueRes(dataBean.getResult().getSubjectQuestionVOList());

                        Utils.saveSudentFile(getSpeakPackagePath(speakPackageId), dataBean);

                        initData(dataBean);
                    }

                    @Override
                    public void fail(String errorCode) {

                    }
                }).dealData();

            }
        });
    }


    public int getCompeleteNum() {
        return completeNum;
    }


    public int getTotalNum() {
        return totalNum;
    }

    CartoonCompletePopupWindow cartoonCompletePopupWindow;

    public void addCompeleteNum() {

        if (completeNum == 4) {
            completeNum = 4;
        } else {
            completeNum++;

            if (completeNum == 4) {
                if (cartoonCompletePopupWindow == null) {
                    cartoonCompletePopupWindow = new CartoonCompletePopupWindow(this);
                }
                cartoonCompletePopupWindow.showPopupWindow();
            }

        }

        finishWorkNum.setText("" + completeNum);
    }

    private void initData(ListenSpeakDetailDataBean dataBean) {
        List<CartoonStudentSubjectiveDataBean> itemData = dataBean.getResult().getSubjectQuestionVOList();
        workTitle.setText(dataBean.getResult().getSpeakPackageName());
        workTotalNum.setText("/" + itemData.size());
        for (int i = 0; i < itemData.size(); i++) {
            CartoonStudentSubjectiveDataBean subjectQuestionVOListBean = itemData.get(i);
            switch (subjectQuestionVOListBean.getCategoryId()) {
                case "7":
//                    fragments.add(CartoonWordSpeakFragment.newInstance(subjectQuestionVOListBean.getSubjectQuestionId() + "", subjectQuestionVOListBean.getQuestionName(), dataBean.getResult().getSpeakPackageId(), packagePath, (ArrayList<CartoonStudentSubjectiveDataBean.StemsListBean>) subjectQuestionVOListBean.getStemsList()));
                    break;
                case "8":
//                    fragments.add(CartoonSentenceSpeakFragment.newInstance(dataBean.getResult().getSpeakPackageId(), subjectQuestionVOListBean.getSubjectQuestionId() + "", subjectQuestionVOListBean.getQuestionName(), subjectQuestionVOListBean.getSubtitleJsonStr(), packagePath, (ArrayList<CartoonStudentSubjectiveDataBean.StemsListBean>) subjectQuestionVOListBean.getStemsList()));
                    break;
                case "9":
//                    fragments.add(CartoonPicSpeakFragment.newInstance(subjectQuestionVOListBean.getSubjectQuestionId() + "", subjectQuestionVOListBean.getQuestionName(), dataBean.getResult().getSpeakPackageId(), packagePath, (ArrayList<CartoonStudentSubjectiveDataBean.StemsListBean>) subjectQuestionVOListBean.getStemsList()));
                    break;
                case "10":
//                    fragments.add(CartoonSongFragment.newInstance(dataBean.getResult().getSpeakPackageId(), subjectQuestionVOListBean.getSubjectQuestionId() + "", subjectQuestionVOListBean.getQuestionName(), subjectQuestionVOListBean.getSubtitleJsonStr(), packagePath, (ArrayList<CartoonStudentSubjectiveDataBean.StemsListBean>) subjectQuestionVOListBean.getStemsList()));
                    break;
                case "11":
                    fragments.add(CartoonExpandVideoFragment.newInstance((ArrayList<CartoonStudentSubjectiveDataBean.StemsListBean>) subjectQuestionVOListBean.getStemsList()));
                    break;
            }
        }

        LogUtil.MyLog("fragments", "====fragments====" + fragments.size());
        mFragmentAdapteradapter = new FragPagerAdapter(getSupportFragmentManager(), fragments);
        //给ViewPager设置适配器
        viewPager.setAdapter(mFragmentAdapteradapter);
    }

    private String getSpeakPackagePath(String speakPackageId) {
        return packagePath + "/resource/" + speakPackageId + ".txt";
    }

    @Override
    public void onWorkSpeakListener(boolean isDO, int totalNum, String title) {
        if (isDO) {
            rightQues();
        }
    }
}
